home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cnetshar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  5.6 KB  |  248 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /*
  18. ** CNetworkConnectionInformation stuff
  19. */
  20.  
  21. IMPLEMENT_SERIAL( CNetworkShareInformation, CObject, 1 )
  22.  
  23. CNetworkShareInformation::CNetworkShareInformation()
  24. {
  25.    m_Initialize();
  26. }
  27.  
  28. CNetworkShareInformation::CNetworkShareInformation( SHARE_INFO_2 *source )
  29. {
  30.    Copy( source );
  31. }
  32.  
  33. CNetworkShareInformation::~CNetworkShareInformation()
  34. {
  35.    m_Initialize();
  36. }
  37.  
  38. void CNetworkShareInformation::Copy( SHARE_INFO_2 *source )
  39. {
  40.    ASSERT( source != NULL );
  41.  
  42.    if ( source == NULL )
  43.    {
  44.       m_Initialize();
  45.       return;
  46.    }
  47.  
  48. #if ! defined( UNICODE )
  49.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_netname, source->shi2_netname );
  50.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_remark,  source->shi2_remark  );
  51.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_path,    source->shi2_path    );
  52.    ::UNICODE_to_ASCII( (LPCWSTR) source->shi2_passwd,  source->shi2_passwd  );
  53. #endif
  54.  
  55.    NetworkName         = source->shi2_netname;
  56.    Type                = source->shi2_type;
  57.    Remark              = source->shi2_remark;
  58.    Permissions         = source->shi2_permissions;
  59.    MaximumNumberOfUses = source->shi2_max_uses;
  60.    CurrentNumberOfUses = source->shi2_current_uses;
  61.    PathName            = source->shi2_path;
  62.    Password            = source->shi2_passwd;
  63.  
  64. #if ! defined( UNICODE )
  65.    ::ASCII_to_UNICODE( source->shi2_netname, (LPWSTR) source->shi2_netname );
  66.    ::ASCII_to_UNICODE( source->shi2_remark,  (LPWSTR) source->shi2_remark  );
  67.    ::ASCII_to_UNICODE( source->shi2_path,    (LPWSTR) source->shi2_path    );
  68.    ::ASCII_to_UNICODE( source->shi2_passwd,  (LPWSTR) source->shi2_passwd  );
  69. #endif
  70. }
  71.  
  72. void CNetworkShareInformation::Empty( void )
  73. {
  74.    m_Initialize();
  75. }
  76.  
  77. void CNetworkShareInformation::m_Initialize( void )
  78. {
  79.    NetworkName.Empty();
  80.    Type                = 0;
  81.    Remark.Empty();
  82.    Permissions         = 0;
  83.    MaximumNumberOfUses = 0;
  84.    CurrentNumberOfUses = 0;
  85.    PathName.Empty();
  86.    Password.Empty();
  87. }
  88.  
  89. void CNetworkShareInformation::Serialize( CArchive& archive )
  90. {
  91.    CObject::Serialize( archive );
  92.  
  93.    if ( archive.IsStoring() )
  94.    {
  95.       archive << NetworkName;
  96.       archive << Type;
  97.       archive << Remark;
  98.       archive << Permissions;
  99.       archive << MaximumNumberOfUses;
  100.       archive << CurrentNumberOfUses;
  101.       archive << PathName;
  102.       archive << Password;
  103.    }
  104.    else
  105.    {
  106.       archive >> NetworkName;
  107.       archive >> Type;
  108.       archive >> Remark;
  109.       archive >> Permissions;
  110.       archive >> MaximumNumberOfUses;
  111.       archive >> CurrentNumberOfUses;
  112.       archive >> PathName;
  113.       archive >> Password;
  114.    }
  115. }
  116.  
  117. /*
  118. ** CNetworkConnections Stuff
  119. */
  120.  
  121. IMPLEMENT_SERIAL( CNetworkShares, CNetwork, 1 )
  122.  
  123. CNetworkShares::CNetworkShares()
  124. {
  125.    m_Initialize();
  126. }
  127.  
  128. CNetworkShares::CNetworkShares( LPCTSTR machine_name )
  129. {
  130.    m_Initialize();
  131.    Open( machine_name );
  132. }
  133.  
  134. CNetworkShares::~CNetworkShares()
  135. {
  136.    Close();
  137.    m_Initialize();
  138. }
  139.  
  140. BOOL CNetworkShares::Add( CNetworkShareInformation& share_to_add )
  141. {
  142.    // NetShareAdd
  143.  
  144.    //m_ErrorCode = ::NetShareAdd( (LPTSTR) m_WideMachineName,
  145.  
  146.    if ( m_ErrorCode == NERR_Success )
  147.    {
  148.       return( TRUE );
  149.    }
  150.    else
  151.    {
  152.       return( FALSE );
  153.    }
  154. }
  155.  
  156. void CNetworkShares::Close( void )
  157. {
  158.    CNetwork::Close();
  159.  
  160.    if ( m_2InformationBuffer != NULL )
  161.    {
  162.       ::NetApiBufferFree( m_2InformationBuffer );
  163.       m_2InformationBuffer = NULL;
  164.    }
  165. }
  166.  
  167. BOOL CNetworkShares::Delete( CNetworkShareInformation& share_to_delete )
  168. {
  169.    // NetShareDel
  170.  
  171.    //m_ErrorCode = ::NetShareDel( (LPTSTR) m_WideMachineName,
  172.  
  173.    if ( m_ErrorCode == NERR_Success )
  174.    {
  175.       return( TRUE );
  176.    }
  177.    else
  178.    {
  179.       return( FALSE );
  180.    }
  181. }
  182.  
  183. void CNetworkShares::m_Initialize( void )
  184. {
  185.    m_ErrorCode               = 0;
  186.    m_2InformationBuffer    = NULL;
  187.    m_2ResumeHandle         = 0;
  188.    m_2CurrentEntryNumber   = 0;
  189.    m_2NumberOfEntriesRead  = 0;
  190.    m_2TotalNumberOfEntries = 0;
  191. }
  192.  
  193. BOOL CNetworkShares::Enumerate( void )
  194. {
  195.    if ( m_2InformationBuffer != NULL )
  196.    {
  197.       ::NetApiBufferFree( m_2InformationBuffer );
  198.       m_2InformationBuffer = NULL;
  199.    }
  200.  
  201.    m_2CurrentEntryNumber   = 0;
  202.    m_2NumberOfEntriesRead  = 0;
  203.    m_2ResumeHandle         = 0;
  204.    m_2TotalNumberOfEntries = 0;
  205.  
  206.    m_ErrorCode = ::NetShareEnum( (LPTSTR) m_WideMachineName,
  207.                                           2, 
  208.                               (LPBYTE *) &m_2InformationBuffer,
  209.                                           65535,
  210.                                          &m_2NumberOfEntriesRead,
  211.                                          &m_2TotalNumberOfEntries,
  212.                                          &m_2ResumeHandle );
  213.  
  214.    if ( m_ErrorCode != NERR_Success || m_2InformationBuffer == NULL )
  215.    {
  216.       return( FALSE );
  217.    }
  218.  
  219.    return( TRUE );
  220. }
  221.  
  222. BOOL CNetworkShares::GetNext( CNetworkShareInformation& information )
  223. {
  224.    if ( m_2CurrentEntryNumber < m_2TotalNumberOfEntries )
  225.    {
  226.       information.Copy( &m_2InformationBuffer[ m_2CurrentEntryNumber ] );
  227.       m_2CurrentEntryNumber++;
  228.       return( TRUE );
  229.    }
  230.  
  231.    information.Empty();
  232.    return( FALSE );
  233. }
  234.  
  235. void CNetworkShares::Serialize( CArchive& archive )
  236. {
  237.    CNetwork::Serialize( archive );
  238.  
  239.    if ( archive.IsStoring() )
  240.    {
  241.       archive << m_ErrorCode;
  242.    }
  243.    else
  244.    {
  245.       archive >> m_ErrorCode;
  246.    }
  247. }
  248.